/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service.video;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.dao.video.EleSourceVideoDao;
import com.thinkgem.jeesite.modules.source.dao.video.EleSourceVideoGroupDao;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideo;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideoGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 视频资源管理Service
 * @author ZC
 * @version 2017-06-16
 */
@Service
@Transactional(readOnly = true)
public class EleSourceVideoGroupService extends CrudService<EleSourceVideoGroupDao, EleSourceVideoGroup> {

	@Autowired
	private EleSourceVideoDao eleSourceVideoDao;
	
	public EleSourceVideoGroup get(String id) {
		EleSourceVideoGroup eleSourceVideoGroup = super.get(id);
		if(null != eleSourceVideoGroup){
			eleSourceVideoGroup.setEleSourceVideoList(eleSourceVideoDao.findList(new EleSourceVideo(eleSourceVideoGroup)));
		}
		return eleSourceVideoGroup;
	}
	
	public List<EleSourceVideoGroup> findList(EleSourceVideoGroup eleSourceVideoGroup) {
		return super.findList(eleSourceVideoGroup);
	}
	
	public Page<EleSourceVideoGroup> findPage(Page<EleSourceVideoGroup> page, EleSourceVideoGroup eleSourceVideoGroup) {
		return super.findPage(page, eleSourceVideoGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceVideoGroup eleSourceVideoGroup) {
		super.save(eleSourceVideoGroup);
		for (EleSourceVideo eleSourceVideo : eleSourceVideoGroup.getEleSourceVideoList()){
			if (eleSourceVideo.getId() == null){
				continue;
			}
			if (EleSourceVideo.DEL_FLAG_NORMAL.equals(eleSourceVideo.getDelFlag())){
				if (StringUtils.isBlank(eleSourceVideo.getId())){
					eleSourceVideo.setGroup(eleSourceVideoGroup);
					eleSourceVideo.preInsert();
					eleSourceVideoDao.insert(eleSourceVideo);
				}else{
					eleSourceVideo.preUpdate();
					eleSourceVideoDao.update(eleSourceVideo);
				}
			}else{
				eleSourceVideoDao.delete(eleSourceVideo);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceVideoGroup eleSourceVideoGroup) {
		super.delete(eleSourceVideoGroup);
		eleSourceVideoDao.delete(new EleSourceVideo(eleSourceVideoGroup));
	}
	
}